# encoding: utf-8

import json
from core.mongo_clinet import Mongo

mongo_uri = "mongodb://root:Dangerous!@192.168.10.99:27017"
mongo_db = "eclink_staples"

mongo = Mongo(mongo_uri=mongo_uri, mongo_db=mongo_db)

goods_fields = {"brand": 1, "brandModel": 1, "brandSpec": 1, "skuName": 1, "skuId": 1}

goods_list = mongo.list(collection="ecl_goods_pool", fields=goods_fields)
# {'_id': '7700221178EA', 'brand': '创维/SKYWORTH', 'brandModel': 'B10JB', 'brandSpec': '43寸', 'skuId': '7700221178EA', 'skuName': '创维 B10JB 落地广告机 43寸'}

binding_fields = {"bindSkuName": 1, "bindSkuUrl": 1, "originPlatSkuId": 1, "originPlatUrl": 1}

binding_list = mongo.list(collection="ecl_binding_pool", fields=binding_fields, query={"bindPlatCode": "jd"})
# {'_id': '1100705552EA_jd:100039554695', 'bindSkuName': '闪迪（SanDisk）64GB Type-C USB3.1手机U盘DDC3至尊高速酷柔 传输速度150MB/s 双接口设计 SDDDC3-064G-Z46', 'bindSkuUrl': 'https://item.jd.com/100039554695.html', 'originPlatSkuId': '1100705552EA', 'originPlatUrl': 'http://www.stbchina.cn/item.html#?itemId=1100705552EA'}

goods_dict = {}
for goods in goods_list:
    sku_id = goods.get("skuId", "")
    if not sku_id:
        continue
    goods_dict[sku_id] = goods


def update_data(bind, goods) -> dict:
    if bind:
        for key, value in bind.items():
            goods[key] = value
    return goods


result = []

for bind in binding_list:
    ori_sku = bind.get("originPlatSkuId", "")
    if not ori_sku:
        continue
    try:
        goods = goods_dict.get(ori_sku, {})
        goods = update_data(bind, goods)
        result.append(goods)
    except Exception as e:
        print("bind:", bind)

with open("./datas.json", "w", encoding="utf-8") as f:
    json.dump(result, f, ensure_ascii=False)

print("all done")
mongo.close()
